package com.jd.finance.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jd.finance.core.mapper.BankMapper;
import com.jd.finance.core.mapper.LoanMapper;
import com.jd.finance.core.pojo.dto.BankMoneyDTO;
import com.jd.finance.core.pojo.dto.LoanProductDTO;
import com.jd.finance.core.pojo.dto.ProductDealNumberByCityDTO;
import com.jd.finance.core.pojo.entity.Bank;
import com.jd.finance.core.service.BankService;
import com.jd.finance.result.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 银行表 服务实现类
 * </p>
 *
 * @author LiQingBo
 * @since 2022-05-17
 */
@Service
public class BankServiceImpl extends ServiceImpl<BankMapper, Bank> implements BankService {

    @Autowired
    private BankMapper bankMapper;

    @Autowired
    private LoanMapper loanMapper;
    /**
     * 查询平台交易银行数量
     * @return
     */
    @Override
    public BaseResult getBankCount() {
        QueryWrapper<Bank> qw=new QueryWrapper<>();
        qw.select("bid");
        int count=bankMapper.selectCount(qw);
        if (count==0) {
            BaseResult.error().message("未查询到交易银行的数量");
        }
        return BaseResult.ok().data("jiaoyiyinhangbank",count);
    }

    /**
     * 根据地区查询所有可贷款银行
     * @param City
     * @return
     */
    @Override
    public BaseResult getLoadBank(String City) {
        if (StringUtils.isBlank(City)) {
            log.error("地址信息为空");
            return BaseResult.error().message("地址信息为空");
        }
        List<Bank> banks = bankMapper.selectLoadBank(City);
        return BaseResult.ok().data("banks",banks).message("查询成功");
    }

    /**
     * 获取各银行当月贷款总金额
     * @param uid
     * @return
     */
    @Override
    public BaseResult getBankLoanMonth(Integer uid) {
        if (uid==null) {
            log.error("id为空!");
            return BaseResult.error().message("id为空");
        }
        List<BankMoneyDTO> bankMoneyDTOS = bankMapper.selectBankMoney(uid);
        if (bankMoneyDTOS == null && bankMoneyDTOS.size() == 0){
            log.error("未查询到银行贷款金额!");
            return BaseResult.error().message("银行本月贷款金额为0!");
        }
        return BaseResult.ok().data("list:",bankMoneyDTOS);
    }

    /**
     * 根据地区名称，获取全年该地区贷款产品成交数量
     * @param uid
     * @param bankCity
     * @return
     */
    @Override
    public BaseResult getProductDealNumberByCity(Integer uid, String bankCity) {
        if(uid == null){
            log.error("uid为空");
            return BaseResult.error().message("uid为空");
        }
        if(StringUtils.isBlank(bankCity)){
            log.error("地区为空");
            return BaseResult.error().message("地区为空");
        }

        Bank bank = new Bank();
        bank.setBankCity(bankCity);
        List<ProductDealNumberByCityDTO> pdn =bankMapper.selectDealNumber(bankCity);
        if(pdn == null){
            log.error("无记录");
            return BaseResult.error().message("无记录");
        }
        return BaseResult.ok().data("list",pdn);
    }


    /**
     * 根据银行id查询  可贷款产品
     * @param bid
     * @return
     */
    @Override
    public BaseResult getLoadProduct(Integer bid) {
        if (bid==null) {
            log.error("银行id为空!");
            return BaseResult.error().message("银行id为空");
        }
        List<LoanProductDTO> loanProductDTOList =bankMapper.selectLoanProductInfo(bid);
        if (loanProductDTOList ==null || loanProductDTOList.size()==0) {
            log.error("未查询到该银行下可贷款产品!");
            return BaseResult.error().message("未查询到该银行下可贷款产品!");
        }
        return BaseResult.ok().data("loanProductList", loanProductDTOList);
    }


}
